
package com.phone;

import com.phone.utils.MessageUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xml.sax.InputSource;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

/**
 * Http Demo for Java
 * 参考文档《短信综合信息管理系统_Http接口文档.doc》
 */
public class SendCode {

    //账户
    static String account = "ZD30109";//
    //密码
    static String password = "5xxppbd6";//
    //校验码
    static String veryCode = "glq98wc1nx1i";

    final static String HTTP_URL = "http://120.27.149.106:8030";


    /**
     * 发送普通短信
     *
     * @param mobile 手机号码, 多个号码以英文逗号隔开
     * @param code   短信验证码
     * @return
     * @throws Exception
     */
    public static String sendTextSms(String mobile, String code) throws Exception {

        String address = HTTP_URL + "/service/httpService/httpInterface.do?method=sendMsg";

        String content = "您的验证码为:" + code + " ，请勿向任何人提供短信验证码";
        StringBuilder params = new StringBuilder();
        params.append("username=").append(account);
        params.append("&password=").append(password);
        params.append("&veryCode=").append(veryCode);
        params.append("&mobile=").append(mobile);
        params.append("&content=").append(content);
        params.append("&msgtype=").append("1");
        params.append("&code=").append("UTF-8");
//		params.append("&sendtime=").append("20151001113000"); //发送定时短信

        URL url = new URL(address);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        out.write(params.toString()); //post的关键所在！
        out.flush();
        out.close();
        String temp = "";
        String result = "";
        InputStream is = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        while ((temp = br.readLine()) != null) {
            result += temp;
        }
//        System.out.println(result);
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        byte[] data = new byte[4096];
//        int count = -1;
//        while ((count=is.read(data,0,2048))!=-1)
//            outputStream.write(data,0,count);
//        data = null;
//        String xmlString = new String(outputStream.toByteArray(),"UTF-8");

        System.out.println(result);
        Document document = DocumentHelper.parseText(result);
        Element root = document.getRootElement();
        List<Element> elements = root.elements();
        Element element = elements.get(0);
        String status = element.elementText("status");
        System.out.println("status = "+status);
        return status;

//        return MessageUtil.parseXml(xmlString);

    }

    /**
     * 发送模版短信(短信模版的创建参考客户端操作手册)
     * <p>
     * 模版：@1@会员，动态验证码@2@(五分钟内有效)。请注意保密，勿将验证码告知他人。
     * 参数值:@1@=member,@2@=4293
     * 最终短信内容：【短信签名】member会员，动态验证码4293(五分钟内有效)。请注意保密，勿将验证码告知他人。
     *
     * @param mobile  手机号码
     * @param tempId  模版编号，对应客户端模版编号
     * @param content 各参数值，以英文逗号隔开，如：@1@=member,@2@=4293
     * @return
     * @throws Exception
     */
    public static String sendTemplateSms(String mobile, String tempId, String content) throws Exception {
        String address = HTTP_URL + "/service/httpService/httpInterface.do?method=sendMsg";

        StringBuilder params = new StringBuilder();
        params.append("username=").append(account);
        params.append("&password=").append(password);
        params.append("&veryCode=").append(veryCode);
        params.append("&mobile=").append(mobile);
        params.append("&content=").append(content);
        params.append("&msgtype=").append("2");
        params.append("&tempid=").append(tempId);
        params.append("&code=").append("utf-8");

        URL url = new URL(address);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
        out.write(params.toString()); //post的关键所在！
        out.flush();
        out.close();
        String temp = "";
        String result = "";
        InputStream is = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        while ((temp = br.readLine()) != null) {
            result += temp;
        }
        System.out.println(result);

        return result;
    }

    /**
     * 获取短信状态报告
     *
     * @return 返回数据格式：
     * <?xml version="1.0" encoding="UTF-8"?>
     * <sms>
     * <rpt>
     * <mobile>15951977097</mobile>
     * <msgid>7799292837969854465</msgid>
     * <status>MA:0006</status>
     * <time>2015-06-12 15:10:53</time>
     * <extno/>
     * </rpt>
     * <rpt>
     * <mobile>15951977097</mobile>
     * <msgid>7799388495481470979</msgid>
     * <status>MA:0006</status>
     * <time>2015-06-12 15:16:17</time>
     * <extno/>
     * </rpt>
     * </sms>
     * @throws Exception
     */
    public static String queryReport() throws Exception {

        String address = HTTP_URL + "/service/httpService/httpInterface.do?method=queryReport";

        StringBuilder params = new StringBuilder();
        params.append("username=").append(account);
        params.append("&password=").append(password);
        params.append("&veryCode=").append(veryCode);

        URL url = new URL(address);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
        out.write(params.toString()); //post的关键所在！
        out.flush();
        out.close();
        String temp = "";
        String result = "";
        InputStream is = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        while ((temp = br.readLine()) != null) {
            result += temp;
        }
        return result;
    }


    public static String queryMo() throws Exception {

        String address = HTTP_URL + "/service/httpService/httpInterface.do?method=queryMo";

        StringBuilder params = new StringBuilder();
        params.append("username=").append(account);
        params.append("&password=").append(password);
        params.append("&veryCode=").append(veryCode);

        URL url = new URL(address);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
        out.write(params.toString()); //post的关键所在！
        out.flush();
        out.close();
        String temp = "";
        String result = "";
        InputStream is = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        while ((temp = br.readLine()) != null) {
            result += temp;
        }
        return result;
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        String result = "";
        //发送普通短信
        //result = sendTextSms("15000531187", "您的验证码为：3123，请勿向任何人提供短信验证码");

        //发送模版短信
        //result = sendTemplateSms("15000531187", "ZD10003-0001", "@1@=Member,@2@=3918");

        //获取短信状态报告
        //result = queryReport();

        //获取上行短信
        //result = queryMo();

//        System.out.println(result);
        System.out.println(getType(600*1024l*1024l));
    }
    public static String getType(Object o){
        return o.getClass().toString();
    }
}
